package com.javaee.xiangyang.controller.Android;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.ShopCarGoods;
import com.javaee.xiangyang.entity.ShopCar;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.service.AccountService;
import com.javaee.xiangyang.service.ShopCarService;
import com.javaee.xiangyang.vo.ServerResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/ShopCar")
public class ShopCarController {

    @Resource
    ShopCarService shopCarService;

    @Resource
    AccountService accountService;


    /**
     * 接受Android传过来的参数
     * 用户存放商品到购物车
     */
    @PostMapping("/InsertShopCar")
    public <T> ServerResponse<T> insertShopCar(ShopCar shopCar) {
        ServiceResultEnum serviceResultEnum = shopCarService.insertShopCar(shopCar);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            return ServerResponse.creatServerResponseBySuccess();
        }
        return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
    }

    /**
     * 根据购物车ID查找具体信息
     */
    @GetMapping("/FindCarById")
    public ServerResponse<ShopCarGoods> findCarById(ShopCar shopCar) {
        ShopCarGoods carById = shopCarService.findCarById(shopCar);
        if (carById != null) {
            return ServerResponse.creatServerResponseBySuccess(carById);
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.SHOPPING_ITEM_ERROR.getResult());
    }

    /**
     * 根据用户查找购物车内商品
     */
    @GetMapping("/FindCarByUserAccount")
    public ServerResponse<List<ShopCarGoods>> findCarByUserAccount(ShopCar shopCar) {
        User user = accountService.GetUser(new User(shopCar.getShopUserAccount()));
        if (user != null) {
            //用户存在
            List<ShopCarGoods> carByUserAccount = shopCarService.findCarByUserAccount(shopCar);
            return ServerResponse.creatServerResponseBySuccess(carByUserAccount);
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }

    /**
     * 更新购物车中某一商品的数量
     */
    @PostMapping("/UpdateCarNumberByShopId")
    public <T> ServerResponse<T> updateCarNumberByShopId(ShopCar shopCar) {
        User user = accountService.GetUser(new User(shopCar.getShopUserAccount()));
        if (user != null) {
            //用户存在
            ShopCarGoods carById = shopCarService.findCarById(shopCar);
            if (carById != null && carById.getShopCar().getShopUserAccount().equals(shopCar.getShopUserAccount())) {
                //购物车存在  且存在于此用户的购物车内
                if (shopCar.getShopNumber() == 0) {
                    //此处直接删除该条购物车信息
                    shopCarService.deleteCarByUserAccountAndGoodsId(shopCar);
                    return ServerResponse.creatServerResponseBySuccess();
                } else if (shopCar.getShopNumber() < 0) {
                    //此处是非法请求了为0即为数据库对应信息根本不应该存在
                    return ServerResponse.creatServerResponseByFail(ServiceResultEnum.SHOPPING_ITEM_ERROR.getResult());
                } else {
                    shopCar.setShopGoodsId(carById.getGoods().getGoodsId());
                    shopCarService.updateCarNumberBy(shopCar);
                    return ServerResponse.creatServerResponseBySuccess();
                }
            }
            return ServerResponse.creatServerResponseByFail(ServiceResultEnum.SHOPPING_CART_NOT_EXIST.getResult());
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }

    /**
     * 用户删除购物车一条信息
     */
    @PostMapping("/deleteCarByUserAccountAndGoodsId")
    public <T> ServerResponse<T> deleteCarByUserAccountAndGoodsId(ShopCar shopCar) {
        User user = accountService.GetUser(new User(shopCar.getShopUserAccount()));
        if (user != null) {
            //用户存在
            ServiceResultEnum serviceResultEnum = shopCarService.deleteCarByUserAccountAndGoodsId(shopCar);
            if (serviceResultEnum == ServiceResultEnum.SUCCESS)
                return ServerResponse.creatServerResponseBySuccess();
            return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }


    /**
     * 用户清空购物车
     */
    @PostMapping("/deleteCarByUserAccount")
    public <T> ServerResponse<T> deleteCarByUserAccount(ShopCar shopCar) {
        User user = accountService.GetUser(new User(shopCar.getShopUserAccount()));
        if (user != null) {
            //用户非空
            ServiceResultEnum serviceResultEnum = shopCarService.deleteCarByUserAccount(shopCar);
            if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
                return ServerResponse.creatServerResponseBySuccess();
            }
            return ServerResponse.creatServerResponseByFail(ServiceResultEnum.ERROR.getResult());
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }
}
